package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.BusOrder;
import com.ruoyi.business.domain.BusOrderInfo;
import com.ruoyi.business.domain.dto.BusOrderInfoDto;
import com.ruoyi.business.service.IBusOrderInfoService;
import com.ruoyi.business.service.IBusOrderService;
import com.ruoyi.business.service.IBusShopService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单Controller
 *
 * @author lzq
 * @date 2022-10-22
 */
@Api(tags = "订单信息管理")
@RestController
@RequestMapping("/api/business/order")
public class BusOrderController extends BaseController {
    @Autowired
    private IBusOrderService busOrderService;
    @Autowired
    private IBusShopService shopService;
    @Autowired
    private IBusOrderInfoService orderInfoService;

    /**
     * 查询订单列表
     */
    @Anonymous
    @ApiOperation("查询订单列表")
    @PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusOrder busOrder) {
        startPage();
        List<BusOrder> list = busOrderService.selectBusOrderList(busOrder);
        return getDataTable(list);
    }

    /**
     * 查询订单记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/listOrderInfo")
    public TableDataInfo list(BusOrderInfo busOrderInfo)
    {
        List<BusOrderInfo> list = orderInfoService.selectBusOrderInfoList(busOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @ApiOperation("导出订单列表")
    @PreAuthorize("@ss.hasPermi('business:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusOrder busOrder) {
        List<BusOrder> list = busOrderService.selectBusOrderList(busOrder);
        ExcelUtil<BusOrder> util = new ExcelUtil<BusOrder>(BusOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @Anonymous
    @ApiOperation("获取订单详细信息")
    @PreAuthorize("@ss.hasPermi('business:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(busOrderService.selectBusOrderById(id));
    }

    /**
     * 新增订单
     */
    @ApiOperation("新增订单")
    @PreAuthorize("@ss.hasPermi('business:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusOrder busOrder) {
        return toAjax(busOrderService.insertBusOrder(busOrder));
    }

    /**
     * 结账
     */
    @Anonymous
    @ApiOperation("结账")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/orderEnd")
    public AjaxResult orderEnd(@RequestBody @Validated BusOrderInfoDto dto) {
        if (dto.getOrderInfoList().size()==0){
            return toAjax(1);
        }
        //计算总价
        BigDecimal decimals = BigDecimal.ZERO;
        for (BusOrderInfo info : dto.getOrderInfoList()) {
            //减少库存
            shopService.reduceStock(info.getShopId(),info.getNumber());
            decimals = decimals.add(info.getTotalPrice());
        }
        //保存订单
        BusOrder order = new BusOrder();
        order.setName(SecurityUtils.getUsername());
        order.setNumber(OrderUtils.getOrderCode(SecurityUtils.getUserId().intValue()));
        order.setTotalPrice(decimals);
        int i = busOrderService.insertBusOrder(order);
        //保存订单详情
        order.setUserId(SecurityUtils.getUserId());
        for (BusOrderInfo info : dto.getOrderInfoList()) {
            info.setOrderId(order.getId());
            orderInfoService.insertBusOrderInfo(info);
        }
        return toAjax(i);
    }

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
    @PreAuthorize("@ss.hasPermi('business:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusOrder busOrder) {
        return toAjax(busOrderService.updateBusOrder(busOrder));
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
    @PreAuthorize("@ss.hasPermi('business:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busOrderService.deleteBusOrderByIds(ids));
    }

}
